package fpt.edu.vn.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import fpt.edu.vn.dao.AddnewDao;
import fpt.edu.vn.dao.TopicsDao;

/* 
 * CompareResultAction.java 
 *  
 * 0.1
 * 
 * 10/15/2012 
 *  
 * Copyright notice 
 *  
 * Modification Logs: 
 *   DATE        AUTHOR      DESCRIPTION 
 *  -------------------------------------------------------- 
 *  15-Oct-2012  ThuongND    Create new 
 */
public class CompareResultAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	Map<String, Object> session;
	private String s_topicId;
	private List<Object> list1;
	private List<Object> list2;
	private List<Object> list3;

	private int favExit1;
	private int favExit2;
	private int favExit3;
	private AddnewDao addnewdao = new AddnewDao();
	private TopicsDao dao = new TopicsDao();

	public String execute() {
		favExit1 = 0;
		favExit2 = 0;
		favExit3 = 0;
		session = ActionContext.getContext().getSession();
		Integer userIdss = 0;
		if (session.containsKey("userIdss")) {
			userIdss = (Integer) session.get("userIdss");
		}
		ArrayList<Integer> arrT = new ArrayList<Integer>();
		session = ActionContext.getContext().getSession();
		StringBuilder topicIds = new StringBuilder();
		if (session.containsKey("topicIds")) {
			topicIds = (StringBuilder) session.get("topicIds");
		}
		String sTopicId = topicIds.toString();

		setS_topicId(sTopicId);
		StringTokenizer st = new StringTokenizer(s_topicId, ",");
		while (st.hasMoreTokens()) {
			arrT.add(Integer.parseInt(st.nextToken().trim()));
		}

		if (arrT.size() == 1) {
			this.list1 = addnewdao.detailToCompare(arrT.get(0));
			if (userIdss != 0) {
				if (dao.checkExitFav(arrT.get(0), userIdss).size() == 1) {
					favExit1 = 1;
				}
			}
		}
		if (arrT.size() == 2) {
			this.list1 = addnewdao.detailToCompare(arrT.get(0));
			this.list2 = addnewdao.detailToCompare(arrT.get(1));
			if (userIdss != 0) {
				if (dao.checkExitFav(arrT.get(0), userIdss).size() == 1) {
					favExit1 = 1;
				}
				if (dao.checkExitFav(arrT.get(1), userIdss).size() == 1) {
					favExit2 = 1;
				}
			}
		}
		if (arrT.size() == 3) {
			this.list1 = addnewdao.detailToCompare(arrT.get(0));
			this.list2 = addnewdao.detailToCompare(arrT.get(1));
			this.list3 = addnewdao.detailToCompare(arrT.get(2));
			if (userIdss != 0) {
				if (dao.checkExitFav(arrT.get(0), userIdss).size() == 1) {
					favExit1 = 1;
				}
				if (dao.checkExitFav(arrT.get(1), userIdss).size() == 1) {
					favExit2 = 1;
				}
				if (dao.checkExitFav(arrT.get(2), userIdss).size() == 1) {
					favExit3 = 1;
				}
			}
		}

		return SUCCESS;

	}

	public String getS_topicId() {
		return s_topicId;
	}

	public void setS_topicId(String s_topicId) {
		this.s_topicId = s_topicId;
	}

	public List<Object> getList1() {
		return list1;
	}

	public void setList1(List<Object> list1) {
		this.list1 = list1;
	}

	public List<Object> getList2() {
		return list2;
	}

	public void setList2(List<Object> list2) {
		this.list2 = list2;
	}

	public List<Object> getList3() {
		return list3;
	}

	public void setList3(List<Object> list3) {
		this.list3 = list3;
	}

	public int getFavExit1() {
		return favExit1;
	}

	public void setFavExit1(int favExit1) {
		this.favExit1 = favExit1;
	}

	public int getFavExit2() {
		return favExit2;
	}

	public void setFavExit2(int favExit2) {
		this.favExit2 = favExit2;
	}

	public int getFavExit3() {
		return favExit3;
	}

	public void setFavExit3(int favExit3) {
		this.favExit3 = favExit3;
	}

}
